package com.wephone.Student.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import com.wephone.Student.R;

public class NewGroupActivity1 extends BaseActivity{
	private EditText groupName;
	private EditText groupDesc;
	private CheckBox groupType1;
	private CheckBox groupType2;
	private CheckBox groupType3;
	private int groupType = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_group_1);
		Bundle bundle = getIntent().getExtras();
		
		init();
		
	}
	
	private void init(){
		groupName = (EditText)findViewById(R.id.group_name);
		groupDesc = (EditText)findViewById(R.id.group_desc);
		
		groupType1 = (CheckBox)findViewById(R.id.groupType1);
		groupType2 = (CheckBox)findViewById(R.id.groupType2);
		groupType3 = (CheckBox)findViewById(R.id.groupType3);
		final float scale = this.getResources().getDisplayMetrics().density;
		groupType1.setPadding(groupType1.getPaddingLeft() + (int)(12.0f * scale + 0.5f),
				groupType1.getPaddingTop(),
				groupType1.getPaddingRight(),
				groupType1.getPaddingBottom());
		groupType2.setPadding(groupType2.getPaddingLeft() + (int)(12.0f * scale + 0.5f),
				groupType2.getPaddingTop(),
				groupType2.getPaddingRight(),
				groupType2.getPaddingBottom());
		groupType3.setPadding(groupType3.getPaddingLeft() + (int)(12.0f * scale + 0.5f),
				groupType3.getPaddingTop(),
				groupType3.getPaddingRight(),
				groupType3.getPaddingBottom());
		
		
		groupType1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if(arg1){
					groupType2.setChecked(false);
					groupType3.setChecked(false);
					groupType = 1;
				}else{
					groupType = 0;
				}
			}
		});
		groupType2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
						// TODO Auto-generated method stub
						if(arg1){
							groupType3.setChecked(false);
							groupType1.setChecked(false);
							groupType = 2;
						}else{
							groupType = 0;
						}
					}
				});
		groupType3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if(arg1){
					groupType1.setChecked(false);
					groupType2.setChecked(false);
					groupType = 3;
				}else{
					groupType = 0;
				}
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//startActivity(new Intent(getApplicationContext(),LoginActivity.class));
				finish();
			}
		});
		
		
		findViewById(R.id.btn_next_step).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(TextUtils.isEmpty(groupName.getText().toString())){
					Toast.makeText(getApplicationContext(), "请输入群名称", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(groupDesc.getText().toString())||(groupDesc.getText().toString()!=null &&groupDesc.getText().toString().length()<5)){
					Toast.makeText(getApplicationContext(), "群介绍不得少于5个字", Toast.LENGTH_SHORT).show();
					return;
				}
				if(groupType==0){
					Toast.makeText(getApplicationContext(), "请选择群组类别", Toast.LENGTH_SHORT).show();
					return;
				}
			
				Bundle bundle = new Bundle();
				bundle.putString("groupName", groupName.getText().toString());
				bundle.putString("groupDesc", groupDesc.getText().toString());
				bundle.putInt("groupType", groupType);
				
				startActivityForResult(new Intent(getApplicationContext(),NewGroupActivity2.class).putExtras(bundle),100);
				
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 100 && resultCode==3){
			finish();
		}
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true; 
       }
       return super.onKeyDown(keyCode, event);
	}
	
}
